<template>
  <div class="approval">
    <el-card class="box-card" style="margin-top: 110px;">
      <div class="title">{{$t('lenders.title1')}}</div>
      <div class="menu-search">
        <div class="menu-title">
          <!-- 业务ID -->
          <div class="searchItem">
            <span>{{$t('lenders.orderId')}}</span>
            <el-input v-model="listQuery.orderId" :placeholder="$t('rule.orderId')" size="small"></el-input>
          </div>
          <!-- 手机号 -->
          <div class="searchItem">
            <span>{{$t('lenders.phoneNo')}}</span>
            <el-input v-model="listQuery.phoneNo" :placeholder="$t('rule.phoneNo')" size="small"></el-input>
          </div>
          <!-- 客户姓名 -->
          <div class="searchItem">
            <span>{{$t('lenders.realName')}}</span>
            <el-input v-model="listQuery.realName" :placeholder="$t('rule.realName')" size="small"></el-input>
          </div>
          <!-- 账龄 -->
          <div class="searchItem">
            <span>{{$t('dispatch.AccountAge')}}</span>
            <el-select v-model="listQuery.billAge" size="small">
              <el-option
                v-for="item in agingArr"
                :key="item.dictCode"
                :label="item.dictDesc"
                :value="item.dictCode"
              ></el-option>
            </el-select>
          </div>
          <!-- 逾期天数 -->
          <!-- <div class="searchItem searchItem-overdue">
            <span>{{$t('dispatch.overDay')}}</span>
            <el-input v-model="listQuery.overDayBegin" size="small"></el-input>
            <b
              style="font-weight: normal; margin: 0 10px; font-size: 12px; color: #5f5f61;"
            >{{$t('lenders.go')}}</b>
            <el-input v-model="listQuery.overDayEnd" size="small"></el-input>
            <b
              style="font-weight: normal; margin-left: 10px;font-size: 12px; color: #5f5f61;"
            >{{$t('dispatch.day')}}</b>
          </div>-->
        </div>
        <div class="menu-title-btn">
          <button type="button" @click="search">{{$t('btn.query')}}</button>
          <button type="button" @click="reset">{{$t('btn.reset')}}</button>
          <button
            type="button"
            :disabled="expDis"
            @click="handleDownload"
            v-if="dispatch_btn_export"
          >{{$t('btn.export')}}</button>
          <button
            type="button"
            :disabled="expDis"
            @click="handleViewExcelUrl"
            v-if="dispatch_btn_black_export"
          >{{$t('loanDetails.cxDcdz')}}</button>
        </div>
      </div>
    </el-card>
    <el-card class="box-card" style="margin-top: 10px;margin-bottom: 20px;">
      <div class="title">{{$t('dispatch.taskList')}}</div>
      <el-table
        v-loading="listLoading"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 95%"
        class="tables"
        border
        stripe
        size="mini"
      >
        <!-- 业务ID -->
        <el-table-column
          prop="orderId"
          align="center"
          header-align="center"
          :label="$t('lenders.orderId')"
          width="140"
        >
          <template slot-scope="scope">
            <span class="href" @click="hrefFn(scope.row)">{{scope.row.orderId}}</span>
          </template>
        </el-table-column>
        <!-- 客户姓名 -->
        <el-table-column
          prop="realName"
          align="center"
          header-align="center"
          :label="$t('lenders.realName')"
          width="180"
        ></el-table-column>
        <!-- 手机号 -->
        <el-table-column
          prop="appUserName"
          align="center"
          header-align="center"
          :label="$t('lenders.phoneNo')"
          width="120"
        ></el-table-column>
        <!-- 产品名称 -->
        <el-table-column
          prop="proName"
          align="center"
          header-align="center"
          :label="$t('dispatch.proName')"
          width="90"
        ></el-table-column>
        <!-- 借款金额 -->
        <el-table-column
          prop="approvedAmt"
          align="center"
          header-align="center"
          :label="$t('reduDetail.loanAmt')"
          width="120"
          :formatter="cashFormatter"
        ></el-table-column>
        <!-- 申请时间 -->
        <el-table-column
          prop="orderCrtTime"
          align="center"
          header-align="center"
          :label="$t('dispatch.orderCrtTime')"
          width="100"
        ></el-table-column>
        <!-- 到期时间 -->
        <el-table-column
          prop="lastPayDate"
          align="center"
          header-align="center"
          :label="$t('dispatch.lastPayDate')"
          width="100"
        ></el-table-column>
        <!-- 首次到期时间 -->
        <el-table-column
          prop="firstDueDate"
          align="center"
          header-align="center"
          :label="$t('dispatch.FirstDueDate')"
          width="150"
        ></el-table-column>
        <!-- 账龄 -->
        <el-table-column
          prop="billAge"
          align="center"
          header-align="center"
          :label="$t('dispatch.AccountAge')"
          width="90"
        ></el-table-column>
        <!-- 订单状态 -->
        <el-table-column
          prop="orderStatus"
          align="center"
          header-align="center"
          :label="$t('dispatch.orderStatus')"
          width="90"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.orderStatus === 1">{{$t('approval.audit')}}</span>
            <span v-else-if="scope.row.orderStatus === 2">{{$t('approval.credit')}}</span>
            <span v-else-if="scope.row.orderStatus === 3">{{$t('approval.repayments')}}</span>
            <span v-else-if="scope.row.orderStatus === 4">{{$t('approval.refused')}}</span>
            <span v-else-if="scope.row.orderStatus === 5">{{$t('approval.clear')}}</span>
            <span v-else-if="scope.row.orderStatus === 6">{{$t('dispatch.orderStatus1')}}</span>
            <span v-else-if="scope.row.orderStatus === 7">{{$t('dispatch.orderStatus2')}}</span>
            <span v-else-if="scope.row.orderStatus === 8">{{$t('orderQuery.status8')}}</span>
            <span v-else-if="scope.row.orderStatus === 9">{{$t('orderQuery.status9')}}</span>
          </template>
        </el-table-column>
        <!-- 借款App -->
        <el-table-column
          prop="smsHeader"
          align="center"
          header-align="center"
          :label="$t('approvalDetail.loanApp')"
          width="120"
        ></el-table-column>
        <!-- 业务渠道 -->
        <el-table-column
          prop="appShortName"
          align="center"
          header-align="center"
          :label="$t('dispatch.appShortName')"
          width="90"
        ></el-table-column>
        <!-- 其他参数 -->
        <el-table-column
          header-align="center"
          align="center"
          :label="$t('dispatch.others')"
          width="100"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="handleClick(scope.row)"
            >{{$t('approvalDetail.hitsee')}}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="listQuery.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
    <el-dialog
      :title="$t('dispatch.others')"
      :visible.sync="centerDialogVisible"
      width="40%"
      center
    >
      <el-table :data="otherData" style="width: 95%" border size="mini">
        <!-- 应还金额 -->
        <el-table-column
          prop="repayAmtNew"
          align="center"
          header-align="center"
          :label="$t('collDetail.shouldAmt')"
          :formatter="cashFormatter"
        ></el-table-column>
        <!-- 已还金额 -->
        <el-table-column
          prop="backMoney"
          align="center"
          header-align="center"
          :label="$t('dispatch.AmtPaidBack')"
          :formatter="cashFormatter"
        ></el-table-column>
        <!-- 未还金额 -->
        <el-table-column
          prop="repayAmt"
          align="center"
          header-align="center"
          :label="$t('dispatch.UnpaidAmt')"
          :formatter="cashFormatter"
        ></el-table-column>
        <!-- 减免金额 -->
        <el-table-column
          prop="manualAccountingAmt"
          align="center"
          header-align="center"
          :label="$t('dispatch.ReductAmt')"
          :formatter="cashFormatter"
        ></el-table-column>
      </el-table>
      <div class="dialog-footer" style="text-align: center;padding-top: 20px;padding-bottom: 10px;">
        <el-button type="primary" @click="centerDialogVisible = false">{{$t('btn.close')}}</el-button>
      </div>
    </el-dialog>
    <!-- 查看Excel URL -->
    <el-dialog :title="$t('loanDetails.cxDcdz')" :visible.sync="urlDialogVisible" width="60%" center>
      <div style="padding-top: 20px;">
        <el-table :data="urlList" border stripe style="width: 100%">
          <!-- 文件名-->
          <el-table-column
            header-align="center"
            align="center"
            prop="fileName"
            :label="$t('loanDetails.wJm')"
          ></el-table-column>
          <!-- 地址 -->
          <el-table-column header-align="center" align="center" :label="$t('loanDetails.xZdz')">
            <template slot-scope="scope">
              <a
                :href="scope.row.href"
                class="href"
                target="_blank"
              >{{$t('dispatch.download')}}</a>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="dialog-footer" style="text-align: center;padding-top: 20px;padding-bottom: 10px;">
        <el-button type="primary" @click="urlDialogVisible = false">{{$t('btn.close')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { parseURL, dataMasking, formatCash } from "utils/index";
import {
  page,
  collectQueryDetail,
  collectQueryPageDetail,
  getDict,
  collectQuery,
  exportUrl
} from "api/collection/dispatch/index";
export default {
  data() {
    return {
      dispatch_btn_export: false,
      dispatch_btn_black_export: false,
      otherData: [],
      centerDialogVisible: false,
      urlDialogVisible: false,
      expDis: true,
      listLoading: false,
      listQuery: {
        limit: 5, //每页条数
        page: 1, //当前页数
        orderId: "", //订单号
        realName: "", //姓名
        phoneNo: "", //手机
        billAge: "", //账龄
        overDayBegin: "",
        overDayEnd: ""
      },
      total: null,
      tableData: [],
      lanStatus: null,
      agingArr: [],
      urlList: []
    };
  },
  created() {
    if (this.language === "zh") {
      this.lanStatus = 1;
    } else if (this.language === "en") {
      this.lanStatus = 2;
    } else {
      this.lanStatus = 3;
    }
    // 账龄
    getDict().then(res => {
      if (res.status === 200) {
        this.agingArr = res.data;
      }
    });
    if (this.collDispPage.has(this.$route.path)) {
      this.listQuery = this.collDispPage.get(this.$route.path);
    }
    this.getList();
    this.dispatch_btn_export = this.elements["dispatch:btn_export"];
    this.dispatch_btn_black_export = this.elements["dispatch:btn_black_export"];
  },
  computed: {
    ...mapGetters(["elements", "language", "collDispPage"])
  },
  methods: {
    // 金额加分隔符
    cashFormatter(row, col, cellValue) {
      return formatCash(cellValue);
    },
    // 查看
    handleClick(row) {
      let obj = {};
      obj.orderId = row.orderId;
      collectQueryPageDetail(obj).then(res => {
        if (res.status === 200) {
          this.otherData = [];
          this.otherData.push(res.data);
          this.centerDialogVisible = true;
        }
      });
    },
    hrefFn(row) {
      let obj = {};
      obj.orderId = row.orderId;
      collectQueryDetail(obj).then(res => {
        if (res.status === 200) {
          row.callResult = res.data.callResult;
          row.callFailedReason = res.data.callFailedReason;
          row.dealResult = res.data.dealResult;
          row.collectRemark = res.data.collectRemark;
          row.channel = res.data.channel;
          this.$store.dispatch("setCollQueryDetail", row);
          this.$router.push({
            path: "/CollectionReport/collQueryDetail",
            name: "催收查询详情"
          });
        }
      });
    },
    // 执行导出后台
    handleDownload() {
      this.agingArr.forEach(item => {
        if (this.listQuery.billAge === item.dictCode) {
          this.listQuery.overDayBegin = item.attr1;
          this.listQuery.overDayEnd = item.attr2;
        } else if (this.listQuery.billAge === "") {
          this.listQuery.overDayBegin = "";
          this.listQuery.overDayEnd = "";
        }
      });
      this.listLoading = true;
      collectQuery(this.listQuery).then(res => {
        this.listLoading = false;
        this.$message({
          message: this.$t("dispatch.exportWarn"),
          type: "success"
        });
        // res.data.forEach(item => {
        //   if (item.realName || item.appUserName) {
        //     item.realName = dataMasking(item.realName);
        //     item.appUserName = dataMasking(item.appUserName);
        //   }
        //   //订单状态
        //   switch (item.orderStatus) {
        //     case 1:
        //       item.orderStatus = this.$t("approval.audit");
        //       break;
        //     case 2:
        //       item.orderStatus = this.$t("approval.credit");
        //       break;
        //     case 3:
        //       item.orderStatus = this.$t("approval.repayments");
        //       break;
        //     case 4:
        //       item.orderStatus = this.$t("approval.refused");
        //       break;
        //     case 5:
        //       item.orderStatus = this.$t("approval.clear");
        //       break;
        //     case 6:
        //       item.orderStatus = this.$t("dispatch.orderStatus1");
        //       break;
        //     case 7:
        //       item.orderStatus = this.$t("dispatch.orderStatus2");
        //       break;
        //     case 8:
        //       item.orderStatus = this.$t("orderQuery.status8");
        //       break;
        //     case 9:
        //       item.orderStatus = this.$t("orderQuery.status9");
        //       break;
        //   }
        // });
        // require.ensure([], () => {
        //   const { export_json_to_excel } = require("@/vendor/Export2Excel");
        //   const tHeader = [
        //     this.$t("lenders.orderId"),
        //     this.$t("lenders.realName"),
        //     this.$t("lenders.phoneNo"),
        //     this.$t("dispatch.proName"),
        //     this.$t("reduDetail.loanAmt"),
        //     this.$t("dispatch.orderCrtTime"),
        //     this.$t("dispatch.lastPayDate"),
        //     this.$t("dispatch.FirstDueDate"),
        //     this.$t("dispatch.AccountAge"),
        //     this.$t("dispatch.orderStatus"),
        //     this.$t("approvalDetail.loanApp"),
        //     this.$t("dispatch.appShortName"),
        //     this.$t("collDetail.shouldAmt"),
        //     this.$t("dispatch.AmtPaidBack"),
        //     this.$t("dispatch.UnpaidAmt"),
        //     this.$t("dispatch.ReductAmt")
        //   ];
        //   const filterVal = [
        //     "orderId",
        //     "realName",
        //     "appUserName",
        //     "proName",
        //     "approvedAmt",
        //     "orderCrtTime",
        //     "lastPayDate",
        //     "firstDueDate",
        //     "billAge",
        //     "orderStatus",
        //     "smsHeader",
        //     "appShortName",
        //     "repayAmtNew",
        //     "backMoney",
        //     "repayAmt",
        //     "manualAccountingAmt"
        //   ];
        //   const list = res.data;
        //   const data = this.formatJson(filterVal, list);
        //   export_json_to_excel(tHeader, data, this.$t("route.dispatch"));
        //   this.listLoading = false;
        // });
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]));
    },
    // 查看导出URL
    handleViewExcelUrl() {
      this.listLoading = true;
      exportUrl().then(res => {
        this.listLoading = false;
        if (res.status === 200) {
          this.urlList = [];
          const urls = res.data || [];
          urls.forEach(url => {
            if (url) {
              this.urlList.push({
                fileName: parseURL(url).file,
                href: url
              });
            }
          });
          this.urlDialogVisible = true;
        }
      });
    },
    getList() {
      this.agingArr.forEach(item => {
        if (this.listQuery.billAge === item.dictCode) {
          this.listQuery.overDayBegin = item.attr1;
          this.listQuery.overDayEnd = item.attr2;
        } else if (this.listQuery.billAge === "") {
          this.listQuery.overDayBegin = "";
          this.listQuery.overDayEnd = "";
        }
      });
      this.listLoading = true;
      this.$store.dispatch("saveCollDispPage", {
        path: this.$route.path,
        listQuery: this.listQuery
      });
      page(this.listQuery).then(res => {
        if (res.status === 200) {
          this.expDis = false;
          this.listLoading = false;
          this.tableData = res.data.rows;
          this.total = Number(res.data.total);
        }
      });
    },
    //查询
    search() {
      this.listQuery.page = 1;
      this.getList();
    },
    //重置
    reset() {
      this.listQuery.limit = 5;
      this.listQuery.page = 1;
      this.listQuery.orderId = "";
      this.listQuery.realName = "";
      this.listQuery.phoneNo = "";
      this.listQuery.billAge = "";
      this.listQuery.overDayBegin = "";
      this.listQuery.overDayBegin = "";
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    }
  }
};
</script>

<style scoped lang='scss'>
.box-card {
  width: 95%;
  margin-left: 2.5%;
  margin-right: 2.5%;
}
.title {
  line-height: 30px;
  font-size: 16px;
  display: flex;
  align-items: center;
  font-weight: bold;
}
.title:before {
  display: block;
  content: "";
  width: 3px;
  height: 16px;
  background-color: #309afe;
}
.title:before {
  margin-right: 8px;
}
::before {
  box-sizing: inherit;
  -webkit-tap-highlight-color: transparent;
}
.menu-title {
  width: 100%;
  box-sizing: border-box;
  background: #ffffff;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  .searchItem {
    display: flex;
    align-items: center;
    padding: 10px 0;
    span {
      display: inline-block;
      width: 100px;
      text-align: right;
      font-size: 12px;
      color: #5f5f61;
      padding-right: 5px;
    }
    div {
      width: 160px;
    }
  }
  .searchItem-date {
    display: flex;
    align-items: center;
    padding: 10px 0;
    b {
      margin: 0 10px;
      font-weight: normal;
      font-size: 12px;
      color: #5f5f61;
    }
    div {
      width: 150px;
    }
  }
  .searchItem-overdue {
    display: flex;
    align-items: center;
    padding: 10px 0;
    div {
      width: 48px;
    }
  }
}
.menu-title-btn {
  padding: 10px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
    &:nth-child(2) {
      background: #ebf5ff;
      color: #20a0ff;
      border: 1px solid #abd5ff;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}
.menu-list {
  padding: 20px 0;
  box-sizing: border-box;
  button {
    padding: 0 20px;
    height: 28px;
    background: #20a0ff;
    color: #fff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
    margin-left: 10px;
    &:nth-child(1) {
      margin-left: 0;
    }
  }
  button[disabled] {
    padding: 0 20px;
    height: 28px;
    background: #c1e0ff;
    color: #fafbff;
    outline: none;
    border-width: 0;
    border-radius: 4px;
    font-size: 12px;
  }
}

.href {
  border-bottom: 1px solid #000;
  cursor: pointer;
  &:hover {
    color: #20a0ff;
    border-bottom: 1px solid #20a0ff;
  }
}
.additional {
  width: 100%;
  margin-top: 10px;
  box-sizing: border-box;
  border: 1px solid #ccc;
  display: flex;
  .additional-item {
    flex: 1;
    text-align: center;
    & > div {
      height: 40px;
      line-height: 40px;
    }
  }
}
</style>